<template>
  <div class="self_adaption">
    <div class="g-container">
      <div class="other">
        <p class="main_text">商城申诉</p>
      </div>
      <!-- 搜索 -->
      <div class="g-search" ref="searchBox">
        <!-- 信息区域 -->
        <div class="all_select">
          <!-- 时间选择区域 -->
          <span class="demonstration">提交时间：</span>
          <el-date-picker
            v-model="searchValue.startAt"
            type="date"
            placeholder="开始时间"
            @change="startTime"
            :clearable="false"
            :default-time="defaultTime(0, 0, 0)" />
          <span class="demonstration" style="display: inline-block; font-size: 12px; color: #898989; margin: 20px"
            >至</span
          >
          <el-date-picker
            v-model="searchValue.endAt"
            type="date"
            placeholder="结束时间"
            @change="endTime"
            :clearable="false"
            :default-time="defaultTime1(23, 59, 59)" />
          &emsp;
          <el-radio-group v-model="radio" class="ml-4" @change="radioSelect">
            <el-radio label="1" size="large">今天</el-radio>
            <el-radio label="2" size="large">昨天</el-radio>
            <el-radio label="3" size="large">近3天</el-radio>
            <el-radio label="4" size="large">近1周</el-radio>
            <el-radio label="5" size="large">近1个月</el-radio>
            <el-radio label="6" size="large">近3个月</el-radio>
            <el-radio label="7" size="large">近1年</el-radio>
          </el-radio-group>
        </div>
        <div class="all_select" style="margin-top: 5px">
          <div style="display: flex; align-items: center">
            <!-- 投诉对象ID -->
            <div class="input_valye">
              <span class="demonstration">投诉对象ID：</span>
              <input
                type="text"
                class="input"
                placeholder="请输入投诉对象ID"
                v-model="searchValue.userId"
                @keyup.enter="search"
                style="width: 180px" />
            </div>
            <!-- 投诉类型 -->
            <div class="input_valye">
              <span class="demonstration">投诉类型：</span
              ><el-select
                v-model="searchValue.type"
                class="m-2"
                @change="search"
                placeholder="请选择优惠券类型"
                style="width: 175px">
                <el-option v-for="item in type_options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
        <!-- 底部按钮区域 -->
        <div class="bottom_btn">
          <el-button class="solid" @click="search">查询</el-button>
          <el-button class="hollow" @click="reset">重置</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table" style="padding: 0px 20px">
        <el-tabs v-model="searchValue.status" @tab-change="tabClick">
          <el-tab-pane label="全部" name="" />
          <el-tab-pane label="待处理" name="0" />
          <el-tab-pane label="处理中" name="1" />
          <el-tab-pane label="已完成" name="2" />
        </el-tabs>
        <!-- 表格区域 -->
        <el-table
          :data="tableData"
          style="width: 100%"
          :header-cell-style="{'text-align': 'center'}"
          :show-overflow-tooltip="true"
          border>
          <el-table-column prop="appealSn" label="申诉单号" align="center" />
          <el-table-column prop="reason" label="申诉原因" align="center" />
          <el-table-column prop="type" label="投诉类型" align="center">
            <template #default="item">
              <span v-if="item.row.type == 0">商品投诉</span>
              <span v-else-if="item.row.type == 1">商家服务投诉</span>
              <span v-else-if="item.row.type == 2">商家承诺履行投诉</span>
              <span v-else="item.row.type == 3">其他</span>
            </template>
          </el-table-column>
          <el-table-column prop="createAt" label="提交时间" align="center" />
          <el-table-column prop="status" label="处理状态" align="center">
            <template #default="item">
              <el-text v-if="item.row.status == 0" type="danger">• 待处理</el-text>
              <el-text v-else-if="item.row.status == 1" type="success">• 处理中</el-text>
              <el-text v-else type="info"> • 已处理</el-text>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300" class="operation">
            <template #default="item">
              <div class="table_btn">
                <el-button size="small" class="none" @click="look(item.row)">查看</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <smartPager
          v-model:current-page="searchValue.page"
          v-model:page-size="searchValue.rows"
          :total="total"
          @change="pageChange" />
      </div>
    </div>
    <!-- 投诉查看 -->
    <div class="appeal" v-if="appeal">
      <div class="title1">
        <div class="title_mes">
          <el-icon size="20" color="#fff">
            <Edit /> </el-icon
          >&nbsp;申诉详情
        </div>
        <span class="close" @click="maskBtn1">✖</span>
      </div>
      <div class="content1">
        <div class="tab_ul">
          <div class="tab_li">
            <div class="tab_tit" style="border-top: 1px solid #e4e7ed">申诉单编号：</div>
            <div class="tab_content" style="border-top: 1px solid #e4e7ed">&emsp;{{ lookMes.appealSn }}</div>
          </div>
          <div class="tab_li">
            <div class="tab_tit">申诉结果：</div>
            <div class="tab_content">&emsp;{{ lookMes.result }}</div>
          </div>
          <div class="tab_li">
            <div class="tab_tit">申诉时间：</div>
            <div class="tab_content">&emsp;{{ lookMes.createAt }}</div>
          </div>
          <div class="tab_li">
            <div class="tab_tit">申诉原因：</div>
            <div class="tab_content">&emsp;{{ lookMes.reason }}</div>
          </div>
          <div class="tab_li">
            <div class="tab_tit" style="height: 150px; line-height: 150px">申诉说明：</div>
            <div class="tab_content especial" style="height: 150px; overflow-y: scroll; padding: 10px">
              &emsp;{{ lookMes.description }}
              <div style="margin-top: 10px" v-if="lookMes.appealImgs">
                <div v-if="lookMes.appealImgs.length != 0">
                  <el-image
                    v-for="(item, index) in lookMes.appealImgs"
                    :key="index"
                    :preview-src-list="[item]"
                    :src="item"
                    class="ph_b"
                    style="width: 100px; height: 100px" />
                </div>
              </div>
            </div>
          </div>
          <div class="tab_li">
            <div class="tab_tit">投诉类型：</div>
            <div class="tab_content">&emsp;</div>
          </div>
          <div class="tab_li">
            <div class="tab_tit">投诉原因：</div>
            <div class="tab_content">&emsp;</div>
          </div>
          <div class="tab_li">
            <div class="tab_tit" style="height: 150px; line-height: 150px">投诉说明：</div>
            <div class="tab_content especial" style="height: 150px; overflow-y: scroll; padding: 10px">
              &emsp;{{ lookMes.description }}
              <div style="margin-top: 10px" v-if="lookMes.complaint.complaintImgs">
                <div v-if="lookMes.complaint.complaintImgs.length != 0">
                  <el-image
                    v-for="(item, index) in lookMes.complaint.complaintImgs"
                    :key="index"
                    :preview-src-list="[item]"
                    :src="item"
                    class="ph_b"
                    style="width: 100px; height: 100px" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="btn_center">
          <el-button class="hollow" @click="maskBtn1">取消</el-button>
        </div>
      </div>
    </div>
    <!-- 遮罩层 -->
    <div id="mask1" v-if="mask" @click="maskBtn1"></div>
  </div>
</template>
<script setup lang="ts">
import {ref, onMounted, reactive, onBeforeUnmount} from 'vue';
import {RequestOrderMes, RequestAppealGetPageList, RequestAppealGetDetail} from '@/api/path/index.js';
import {Edit} from '@element-plus/icons-vue';
import moment from 'moment';
import type {FormInstance} from 'element-plus';
import {ElMessage} from 'element-plus';
import {union} from 'lodash-es';
const activeName = ref('first');
const defaultTime = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
const defaultTime1 = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
const tableData = ref<any>([]); // 表格列表
const total = ref(0); //表格总数
const mask = ref(false); //控制遮罩层显示
const appeal = ref(false);
const radio = ref('');
interface RestaurantItem {
  value: string;
  link: string;
}
const consolidateOrder = ref<any>([]);
const restaurants = ref<RestaurantItem[]>([]);
const loadAll = () => {
  return consolidateOrder.value;
};
// 挂载
onMounted(() => {
  appealGetPageList();
  restaurants.value = loadAll();
  handleResize();
  window.addEventListener('resize', handleResize);
});
const searchBox = ref();
const tablelistHeight = ref();
const handleResize = () => {
  const searchBoxHeight = searchBox.value.clientHeight;
  tablelistHeight.value = 110 + searchBoxHeight + 'px';
  console.log('Search Box height11111111111111111111111111111:', searchBoxHeight);
};
/**
 * 获取申诉列表
 */
const appealGetPageList = async () => {
  const a = await RequestAppealGetPageList(searchValue.value);
  tableData.value = a.resultObj.list;
  total.value = a.resultObj.count;
  console.log(a);
};
// 搜索参数
const searchValue = ref({
  userId: '',
  type: '',
  status: '',
  startAt: '',
  endAt: '',
  shopId: localStorage.getItem('ShopId'),
  page: 1,
  rows: 10,
});
// 开始时间
const startTime = () => {
  searchValue.value.startAt.setHours(0);
  searchValue.value.startAt.setMinutes(0);
  searchValue.value.startAt.setSeconds(0);
  searchValue.value.startAt = moment(searchValue.value.startAt).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '0';
  search()
};
// 结束时间
const endTime = () => {
  console.log('搜索时间', searchValue.value.endAt);

  searchValue.value.endAt.setHours(23);
  searchValue.value.endAt.setMinutes(59);
  searchValue.value.endAt.setSeconds(59);
  searchValue.value.endAt = moment(searchValue.value.endAt).format('yyyy-MM-DD HH:mm:ss');
  console.log('搜索时间1111', searchValue.value.endAt);
  radio.value = '0';
  search()
};
/**
 * 时间范围单选按钮
 */
const radioSelect = () => {
  searchValue.value.startAt = '';
  searchValue.value.endAt = '';
  var now = new Date();
  now.setHours(0);
  now.setMinutes(0);
  now.setSeconds(0);
  var now1 = new Date();
  now1.setHours(23);
  now1.setMinutes(59);
  now1.setSeconds(59);
  searchValue.value.endAt = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  if (radio.value === '1') {
    searchValue.value.startAt = moment(now).format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '2') {
    searchValue.value.startAt = moment(now).subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endAt = moment(now1).subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '3') {
    searchValue.value.startAt = moment(now).subtract(3, 'days').format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '4') {
    searchValue.value.startAt = moment(now).subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '5') {
    searchValue.value.startAt = moment(now).subtract(1, 'months').format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '6') {
    searchValue.value.startAt = moment(now).subtract(3, 'months').format('YYYY-MM-DD HH:mm:ss');
  } else {
    searchValue.value.startAt = moment(now).subtract(1, 'years').format('YYYY-MM-DD HH:mm:ss');
  }
  search()
};
/**
 * 类型选择器
 */
const type_options = [
  {
    value: 0,
    label: '商品投诉',
  },
  {
    value: 1,
    label: '商家服务投诉',
  },
  {
    value: 2,
    label: '商家承诺履行投诉',
  },
  {
    value: 3,
    label: '其他',
  },
];
/**
 * 切换类型
 */
const tabClick = () => {
  appealGetPageList();
};
/**
 * 点击查看
 */
const lookMes = ref();
const look = async (item: any) => {
  const a = await RequestAppealGetDetail(item.id);
  if (a.msg == '操作成功！') {
    console.log('查看详情', a.resultObj);
    appeal.value = true;
    mask.value = true;
    lookMes.value = a.resultObj;
  } else {
    ElMessage.error('操作失败！请重试！');
  }
};

/**
 * 搜索
 */
const search = async () => {
  console.log('zzzzzzzzz', searchValue.value);
  appealGetPageList();
};
/**
 * 点击重置
 */
const reset = () => {
  searchValue.value = {
    userId: '',
    type: '',
    status: '',
    startAt: '',
    endAt: '',
    shopId: localStorage.getItem('ShopId'),
    page: 1,
    rows: 10,
  };
  appealGetPageList();
  radio.value = '0';
};
/**
 * 分页
 */
const pageChange = async (val: number) => {
  appealGetPageList();
};
/**
 * 遮罩层数据
 */
const maskBtn1 = () => {
  appeal.value = false;
  mask.value = false;
};
</script>
<style lang="scss" scoped>
@import '@/assets/css/plateform/Article/Content.css';
@import '@/assets/css/plateform/Shop/GoodsManage.css';

.appeal {
  width: 70%;
  height: 90%;
  background: #ffff;
  position: absolute;
  top: 0;
  left: 15%;
  z-index: 9999;

  .tab_ul {
    width: 90%;
    margin: 40px auto;

    .tab_li {
      display: flex;
      align-items: center;

      .tab_tit {
        width: 100px;
        height: 40px;
        font-size: 14px;
        color: #606266;
        background: #f5f7fa;
        border: 1px solid #e4e7ed;
        text-align: center;
        line-height: 40px;
        border-top: 0;
      }

      .especial::-webkit-scrollbar {
        display: none;
      }

      .tab_content {
        flex: 1;
        height: 40px;
        line-height: 40px;
        font-size: 14px;
        color: #3d3d3d;
        border: 1px solid #e4e7ed;
        border-top: 0;
      }
    }
  }
}

.table {
  height: calc(100% - v-bind(tablelistHeight)) !important;
}

::v-deep .el-table--fit {
  height: calc(100% - 120px) !important;
}

.table :deep(.el-table__cell) {
  text-align: center;
}

::deep(.el-radio-group) {
  width: 100% !important;
  background: pink !important;
}

::v-deep .el-range-editor--small.el-input__wrapper {
  height: 40px !important;
}

.btn {
  width: 150px;
  position: absolute;
  right: 20px;
  bottom: 40px;
}

.close_order :deep(.is-finish .el-step__line) {
  background: #f57373;
}

.el-table .warning-row {
  background: #44a2ff !important;
}

::v-deep .el-table--enable-row-transition .el-table__body td.el-table__cell {
  z-index: initial !important;
}
</style>
